package com.example.withroadlogistics.layers;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.withroadlogistics.BMapApiDemoMain;
import com.example.withroadlogistics.R;
import com.example.withroadlogistics.model.User;
import com.example.withroadlogistics.until.HttpCallbackListener;
import com.example.withroadlogistics.until.HttpUtil;
import com.example.withroadlogistics.until.MySharedPreferences;
import com.example.withroadlogistics.until.constant;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;

/**
 * 报警模块
 * 作者：Administrator on 2020/2/20 0020 00:06
 * 邮箱：1415862429@qq.com
 */
public class PolicePage extends AppCompatActivity implements View.OnClickListener{
    private String TAG = "ifu25";
    private Button mBtLoginSubmit;

    private String originAddress = constant.url+"task/addpolice";
    //    Handler是Android SDK来处理异步消息的核心类。
//    子线程与主线程通过Handler来进行通信。子线程可以通过Handler来通知主线程进行UI更新。
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = "";

            if ("OK".equals(msg.obj.toString())){
                result = "success";
            }else if ("Wrong".equals(msg.obj.toString())){
                result = "fail";
            }else {
                String data = msg.obj.toString();
//            	Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show();
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    String code = jsonObject.getString("code");
                    // if (code.equals("1")) {
                    if (code.equals("0")) {
                        // result = jsonObject.getString("data");
                        //web端返回的json code data
                        Log.i(TAG, "handleMessage: wen端返回的json"+code+data+result);
                        //通过Bundle封装数据
                        Bundle bundle = new Bundle();
                        // bundle.putString("data", result);//data是通过后台返回数值进行判断，弹窗
                        //通过Intent控制Activity跳转
                        Toast.makeText(PolicePage.this, result, Toast.LENGTH_LONG).show();//失败提示
                        Toast.makeText(PolicePage.this, "您以报警成功，请等待反馈", Toast.LENGTH_LONG).show();
                    }else {
                        //  result = jsonObject.getString("data");
                        Toast.makeText(PolicePage.this, result, Toast.LENGTH_LONG).show();//失败提示
                        Toast.makeText(PolicePage.this, "您目前无工单信息", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(PolicePage.this, "系统故障请稍后重试", Toast.LENGTH_LONG).show();
                }
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_police);
        initView();
        mBtLoginSubmit = (Button)findViewById(R.id.bt_police_submit);
    }


    //初始化视图
    private void initView() {
        //提交、注册
        mBtLoginSubmit = findViewById(R.id.bt_police_submit);
        mBtLoginSubmit.setOnClickListener(this);
    }

    private boolean isInputValid() {
        //检查用户输入的合法性，这里暂且默认用户输入合法
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_police_submit:
                login();
                break;
        }
    }

    //登陆
    public void login() {
        //取得用户输入的账号和密码
        if (!isInputValid()){
            return;
        }

        String userId = (String) MySharedPreferences.getUserId(PolicePage.this);
        if (userId.isEmpty()) {
            Toast.makeText(PolicePage.this, "用户失效请重新登陆！", Toast.LENGTH_LONG).show();
            return;
        }
        HashMap<String, String> params = new HashMap<String, String>();
        params.put(User.USERID, userId);

        try {
            String compeletedURL = HttpUtil.getURLWithParams(originAddress, params);
            HttpUtil.sendHttpRequest(compeletedURL, new HttpCallbackListener() {
                @Override
                public void onFinish(String response) {
                    Message message = new Message();
                    message.obj = response;
                    mHandler.sendMessage(message);
                }

                @Override
                public void onError(Exception e) {
                    Message message = new Message();
                    message.obj = e.toString();
                    mHandler.sendMessage(message);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
